<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/**
 * 公共API
 */
Route::group(['namespace' => 'Api'], function () {
    // 获取省市区
    Route::get('/regions/format', 'RegionController@format')->name('regions.format');
});

/**
 * 用户登录
 */
Route::post('/user/login', 'WebApp\Api\UserController@login')->name('user.login');
/**
 * 用户注册
 */
Route::post('/user/register', 'WebApp\Api\UserController@create')->name('user.register');
/**
 * 获取验证码
 */
Route::post('/user/register/authcode', 'WebApp\Api\UserController@authCode')->name('user.register.authcode');

/**
 * 短信重置密码
 */
Route::post('/user/password/reset/mobile/authcode', 'WebApp\Api\UserController@authCodeForResetPassword')->name('user.password.reset.authcode');
Route::post('/user/password/reset/mobile', 'WebApp\Api\UserController@resetPasswordForMobile')->name('user.password.reset');

/**
 * 获取附近设备列表
 */
Route::get('devices', 'WebApp\Api\DeviceController@index')->name('device.list');

// 充值订单微信回调处理
Route::any('orders/charge/wxpay/notify', 'WebApp\Api\OrderController@chargeOrderWxpayNotify')->name('order.charge.wxpay.notify');
Route::any('orders/charge/wxpay/app/notify', 'WebApp\Api\OrderController@chargeOrderAppWxpayNotify')->name('order.charge.wxpay.app.notify');
// 充值订单支付宝回调
Route::any('orders/charge/alipay/notify', 'WebApp\Api\OrderController@chargeOrderAlipayNotify')->name('order.charge.alipay.notify');
Route::any('orders/charge/alipay/return', 'WebApp\Api\OrderController@chargeOrderAlipayReturn')->name('order.charge.alipay.return');


Route::group([
    'middleware' => [ 'refresh-token:api', 'auth:api', 'auth.status:api'],
    'namespace' => 'WebApp\Api'
], function () {
    /**
     * 获取登录用户信息
     */
    Route::get('/user', function (Request $request) {
        $user = $request->user('api');
        if ($user->type == 0) {
            return response()->json(ajax_return(true, '获取用户信息成功', new \App\Http\Resources\UserResource($user)));
        } else {
            return response()->json(ajax_return(false, '获取用户信息失败'));
        }
    })->middleware(['scope:user-info']);

    // 更新用户信息
    Route::patch('users', 'UserController@update')->name('user.update');

    // 修改密码
    Route::patch('/users/password/reset', function (Request $request) {
        $rules = [];
        $user = \App\Tables\User::find($request->user('api')->id);

        // 验证用户密码
        $rules['old_password'] = new \App\Rules\checkOldPasswordRule('users', 'password', $user->id);
        $rules['password'] = "required|string|max:18|min:6";
        $rules['re_password'] = 'same:password';

        Validator::make($request->all(), $rules)->validate();

        $user->password = bcrypt($request->password);
        if ($user->save()) {
            return response()->json(ajax_return(true, '密码修改成功'));
        }

        return response()->json(ajax_return(false, '密码修改失败'));

    })->middleware(['auth:api', 'auth.status:api'])->name('user.general.password.reset');

    /**
     * 获取设备信息
     */
    Route::get('devices/{device_sn}', 'DeviceController@detail')->name('device.detail');

    /**
     * 积分相关
     */
    Route::post('/user/sign/score', 'UserController@signInGiveScore')->name('user.sign.in.give.score');
    Route::get('/user/score/list', 'UserController@scores')->name('user.score.list');

    /**
     * 订单相关
     */
    // 订单列表
    Route::get('orders', 'OrderController@index')->name('order.list');
    // 订单详情
    Route::get('orders/{order_sn}', 'OrderController@detail')->where('order_sn', '^\d{4}.*')->name('order.detail');
    // 充值订单列表
    Route::get('orders/charges', 'OrderController@chargeList')->name('order.charge.list');
    Route::get('orders/charges/config/moneys', function () {
        $moneys = array_values(config('system.charge_moneys', [10, 20, 50, 100, 150, 200]));
        return response()->json(ajax_return(true, 'ok', $moneys));
    });
    // 下单
    Route::post('orders/create', 'OrderController@create')->name('order.create');
    // 设备停止指令
    Route::get('orders/stop/{order_sn}', 'OrderController@stop')->name('order.stop');
    // 用户充值
    Route::post('orders/charge/create', 'OrderController@charge')->name('order.charge.create');
});